﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace DIProject.Util
{
    public class StrihWapper
    {
        public static void PrintBarKode(Int32 with, Int32 heith,Int32 count,Int32 port, string text,string price,string font, string barCode)
        {
            if (barCode.Length != 12)
                throw new Util.DIException("Неверный штрихкод");
            LabelPrint.LabelImageClass l = new LabelPrint.LabelImageClass();            
           // l.DoFormFeed("PORT", port);
            string firmName = DateTime.Now.ToString("d");// DIProject.Util.Setting.FirmName;
            l.BeginLabel(with, heith);
            l.DrawText(1, 0, 30, 20, firmName, font, 10, null, 204, 0, 3, 1, 0, 1);
            l.DrawText(1, 2, 30, 20, text , font, 10, null, 204, 0, 3, 1, 0, 1);
            l.DrawText(1, 6, 30, 20, price, font, 16, "BOLD", 204, 0, 3, 1, 0, 1);
            //l.DrawText(1, 15, 40, 30, price, font, 10, null, 204, 0, 3, 1, 0, 1);
            l.DrawBarcode(1, 9, 30, 17, 0, 2, barCode, 1, 1, font, 10, null, 204);
            //l.PreviewLabel();
            l.EZPrintLabel(count,"PORT",port);
            
        }
        public static void ZebraPrintBarcode(Int32 count, string text, string price, string barCode)
        {
            Int32 l = 23;
            string tmp = text.ToUpper();
            tmp = tmp.Replace("\"", "''");
            Int32 l1 = tmp.Length;
            string twoLine = string.Empty;
            string firstLine = string.Empty;
            //длина меньше равна 23 символов
            if (l1 <= l)
            {
                firstLine = tmp;
                goto print;
            }
            //длина меньше 46 символов
            if (l1 <= (2 * l))
            {
                firstLine = tmp.Substring(0, l);
                Int32 itnp = l1 - l;
                if (itnp > l)
                {
                    twoLine = tmp.Substring(l, l);
                }
                else
                {
                    twoLine = tmp.Substring(l, itnp);
                }
                goto print;
            }
            if (l1 > (2 * l))
            {
                firstLine = tmp.Substring(0, l );
                twoLine = tmp.Substring(l, l);
                goto print;
            }
            print:DriverLib.Device d = DriverLib.Device.FromPath(StrihWapper.LPT, "test");
            d.Open();
            Util.SettingFile sf = Util.Setting2.GetSetting();
            Encoding e = Encoding.GetEncoding(sf.ZebraEncoding);
            StreamWriter sw = new StreamWriter(d.Stream,e);
            string date = DateTime.Now.ToString("d");
            string str = string.Format(Template, date, firstLine, twoLine, price, barCode, count);
            sw.Write(str);
            sw.Close();
            d.Stream.Dispose();
            d.Close(); 
        }
        private static string LPT
        {
            get
            {
                string s = System.Configuration.ConfigurationManager.AppSettings["LPT"];
                if (string.IsNullOrEmpty(s))
                    return "LPT1";
                return s;
            }
        }
        private static string FileTemplatePath
        {
            get
            {
                return Directory.GetCurrentDirectory() + "/Template.txt";
            }
        }
        private static string Template
        {
            get
            {
                if (System.IO.File.Exists(FileTemplatePath))
                {
                    StreamReader sr = null;
                    try
                    {
                        sr = System.IO.File.OpenText(FileTemplatePath);
                        string s = sr.ReadToEnd();
                        return s;
                    }
                    finally
                    {
                        if (sr != null)
                        {
                            sr.Close();
                        }
                    }
                }
                else
                    throw new Exception("Не найден шаблон");
            }
        }
        
    }
}
